let BASE_URL = 'https://d-yuanding.bigchun.com'
// let environment = 'development'
let environment = 'production'
let redirect =''

if (environment === 'development') {
	BASE_URL = 'https://d-yuanding.bigchun.com'
	redirect = "https://d-yuanding.bigchun.com/h5test"
} else {
	BASE_URL = "https://yuanding.bigchun.com"
	redirect = "https://yuanding.bigchun.com"
}

import store from '@/store/index.js'
export const request = (option = {}) => {
	return new Promise((resolve, reject) => {
		let token = uni.getStorageSync("sstoken")
		uni.showLoading({
			mask: true,
			title: "加载中..."
		})
		uni.request({
			url: BASE_URL + option.url,
			method: option.method ?? 'GET',
			data: option.data,
			header: {
				token
			},
			success(res) {
				if (!option.showToast) {
					uni.hideLoading()
				}
				if (res.data.code == 1) {
					resolve(res.data)
				} else if (res.data.code == 401) {
					uni.removeStorageSync("sstoken")
					uni.navigateTo({
						url: '/pages/login/index'
					})
				} else {
					return uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			fail(e) {
				uni.showToast({
					title: '服务器错误',
					icon: 'none'
				})
				reject(e)
			}
		})

	})
}

export const get = (url, data, option = {}) => {
	option.method = 'GET';
	option.data = data;
	option.url = url;
	return request(option)
}
export const post = (url, data, option = {}) => {
	option.method = 'POST';
	option.data = data;
	option.url = url;
	return request(option)
}
export default {
	environment: environment,
	redirect:redirect
}
